import requests
from bs4 import BeautifulSoup
import re
import os
import time
# --about-cloudConfig---------------------------------
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
import json

# --public----------------------------------
page = 25
target = 'https://dribbble.com/shots?page='
method = 'cloudSave'  # localSave/cloudSave
timer = True
# --local----------------------------------


# --fun-main---------------------------------
def DribbbleCover(tar, page):
    res = requests.get(tar)
    res.encoding = 'utf-8'
    if(res.status_code != 200):
        print('status_code:', res.status_code)
    # print('status_code:', res.status_code)
    print('--spiding-', page, '--------------------')
    soup = BeautifulSoup(res.text, "html.parser")
    # print(soup.prettify())
    for dom in soup.find_all('div', attrs={'class': 'dribbble video'}):
        src = dom.attrs['data-video-teaser-xlarge']
        alt = src.split('/')[len(src.split('/'))-1]
        new = True
        if(method == 'localSave'):
            localSave(src, alt, new)
        elif(method == 'cloudSave'):
            cloudSave(src, alt, new)
        else:
            printBoard(src, alt, new)


# --fun-localSave------------------------------
def localSave(src, alt, new):
    print('LS:', alt)
    # get localList
    localList = os.listdir(os.getcwd()+'/box')
    for item in localList:
        if (item.split('dbc_')[1] == alt):
            # print('已经存在了')
            new = False
            break
    if new:
        print('NEW:', alt)
        mp4 = requests.get(src).content
        with open('./box/dbc_' + alt, 'wb') as file:
            file.write(mp4)
        print('--e--------------')


# --fun-cloudSave------------------------------
def cloudSave(src, alt, new):
    with open('list/cloudSave.json', 'r') as listR:
        items = json.load(listR)
        count = len(items)
        for item in items:
            if(item == alt):
                new = False
                break
        if new:
            print('NEW:', alt)
            secret_id = 'AKIDPoO4JejOzKPM4iheaYMBl6BzZqCJmqCb'
            secret_key = '9GVVH403rSiEVdbUDdniY9anlz9ankcd'
            region = 'ap-shanghai'
            token = None
            scheme = 'https'
            config = CosConfig(Region=region, SecretId=secret_id,
                               SecretKey=secret_key, Token=token, Scheme=scheme)
            client = CosS3Client(config)
            # chunk save
            stream = requests.get(src)
            response = client.put_object(
                Bucket='browniu-dbc-1257187612',
                Body=stream,
                Key=alt
            )
            print('save success,', response['ETag'])
            items.append(alt)
            with open('list/cloudSave.json', 'w') as listW:
                json.dump(items, listW)
                # print('write success')
            print('--e-', count+1, '---------------')
# --fun-printBoard-----------------------------


def printBoard(src, alt, new):
    print('PB:', alt)


# --init----------------------------------
def init():
    for i in range(0, page):
        # print(target+str(i))
        DribbbleCover(target+str(i), i+1)
        if (i == page-1):
            localtime = time.asctime(time.localtime(time.time()))
            print('spid over,have fun! |', localtime)

    time.sleep(60*10)
    init()


# --timer----------------------------------
init()
